﻿using CPPEI;
using CPPEI.WebApi;
using SLG.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Ser = SLG.Domain.Services;
using Ent = SLG.Domain.Entities;
using V = SLG.Domain.Views;
using D = SLG.Domain.Datas;

namespace SLG.WebApi.Army
{
    [AuthGetAction("Army", "Get", "1.0")]
    public class GetAction : BaseAction<GetParams, GetResult>
    {
        protected override ActionResult<GetResult> OnExecute(ActionContext context, SysParameters sysParams, GetParams opParams)
        {
            int userId = Convert.ToInt32(context.UserId);

            Ser.QueryService service = new Ser.QueryService();
            SingleResult<D.ArmyDetail> serResult = service.QueryArmy(userId, opParams.ArmyId);

            if (serResult.IsSuccess)
            {
                GetResult actResult = new GetResult();

                actResult.Id = serResult.Data.Army.Id;
                actResult.KingdomId = serResult.Data.Army.KingdomId;
                actResult.CityId = serResult.Data.Army.CityId;
                actResult.MarshalId = serResult.Data.Army.MarshalId;
                actResult.FormationId = serResult.Data.Army.FormationId;
                actResult.Number = serResult.Data.Army.Number;
                actResult.PosX = serResult.Data.Army.PosX;
                actResult.PosY = serResult.Data.Army.PosY;

                for (int index = 0; index < serResult.Data.Units.Count; index++)
                {
                    GetResult.Unit unit = new GetResult.Unit();

                    unit.Id = serResult.Data.Units[index].Id;
                    unit.HeroId = serResult.Data.Units[index].HeroId;
                    unit.SoldierId = serResult.Data.Units[index].SoldierId;
                    unit.Number = serResult.Data.Units[index].Number;
                    unit.Position = serResult.Data.Units[index].Position;
                    actResult.Resources.Add(unit);
                }
                return new ActionResult<GetResult>(actResult);
            }
            else
            {
                return new ActionResult<GetResult>(serResult);
            }
        }
    }

    public class GetParams
    {
        public Guid ArmyId { get; set; }
    }

    public class GetResult
    {
        public Guid Id { get; set; }
        public Guid KingdomId { get; set; }
        public Guid CityId { get; set; }
        public Guid MarshalId { get; set; }
        public int FormationId { get; set; }
        public int Number { get; set; }
        public int PosX { get; set; }
        public int PosY { get; set; }

        public List<Unit> Resources = new List<Unit>();

        public class Unit
        {
            public Guid Id { get; set; }
            public Guid HeroId { get; set; }
            public int SoldierId { get; set; }
            public int Number { get; set; }
            public int Position { get; set; }
        }
    }
}
